﻿using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.ViewModels;
using ShopB2C.Helper;
using ShopB2C.Enums;
using System.IO;

namespace ShopB2C.Controllers.Brower
{
    public partial class CustomerController
    {

        /// <summary>
        /// 地址簿管理
        /// </summary>
        public ActionResult AddressBook()
        {
            this.SiteMapPathAdd("地址簿管理");
            CustomerViewModel _customerViewModel = new CustomerViewModel();
            _customerViewModel.AddressBookList = _userAccess.AddressBookList(_user.Uid);
            return View(_customerViewModel);
        }
        /// <summary>
        /// 保存收货人地址
        /// </summary>
        [HttpPost]
        public ActionResult AddressBook(UserAddressEntity userAddress)
        {
            MessageHelper msg = new MessageHelper("shopcar");
            msg.AddBody(1);
            msg.Required(userAddress.ReallyName , 1);
            msg.Required(userAddress.Address , 2);
            if (msg.IsValid)
            {
                userAddress.Uid = _user.Uid;
                _userAccess.SaveAddressBook(userAddress);
                msg.AddBody(2);
                msg.AddMessage(3);
                msg.DefaultOperate(Url.Action("AddressBook"));
            }
            return MessageResult(msg.CreateMessage());
        }
        /// <summary>
        /// 删除收货人地址
        /// </summary>
        public ActionResult DeleteAddressBookItem([DefaultValue(0)]int addressId)
        {
            int result = _userAccess.DeleteAddressBook(addressId , _user.Uid);
            return new EmptyResult();
        }
        /// <summary>
        /// 注册资料
        /// </summary>
        public ActionResult SelfInfo()
        {
            this.SiteMapPathAdd("注册资料维护");

            ViewData["UserFaceRoot"] = _userFaceRoot;
            return View(_user);
        }
        /// <summary>
        /// 注册资料
        /// </summary>
        [HttpPost]
        public ActionResult SelfInfo(UserEntity userInfo)
        {
            userInfo.Uid = _user.Uid;
            userInfo.Uname = _user.Uname;
            userInfo.Face = Path.GetFileName(userInfo.Face);

            MessageHelper msg = new MessageHelper("customer");
            msg.AddBody(3);
            msg.Required(userInfo.NickName , 4);

            if (msg.IsValid)
            {
                int result = _userAccess.Update(userInfo);
                // 保存成功覆盖本地cookie票据
                if (result > 0)
                    this.WriteLoginCookie(userInfo , 40);
                msg.AddBody(4);
                msg.AddMessage(5);
                msg.DefaultOperate(Url.Action("SelfInfo"));
            }
            return MessageResult(msg.CreateMessage());
        }
        /// <summary>
        /// 用户图标列表
        /// </summary>
        public ActionResult UserFaceList()
        {
            Response.Write("<div>");

            if (Directory.Exists(Server.MapPath(_userFaceRoot)))
                foreach (var item in Directory.GetFiles(Server.MapPath(_userFaceRoot) , "*.jpg"))
                {
                    Response.Write(string.Format("<img src=\"{0}/{1}\" alt=\"\" />" , _userFaceRoot , Path.GetFileName(item)));
                }
            else
                Response.Write("没有找到任何图标文件");
            Response.Write("</div>");
            return new EmptyResult();
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        public ActionResult ChangePassword()
        {
            this.SiteMapPathAdd("密码修改");
            return View();
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        [HttpPost]
        public ActionResult ChangePassword(
            [DefaultValue("")]string oldPassword
            , [DefaultValue("")]string password)
        {

            MessageHelper msg = new MessageHelper("customer");
            msg.AddBody(1);
            msg.MinLength(password , 6 , 1);
            if (msg.IsValid)
            {
                int result = _userAccess.ChangePassword(_user.Uid , oldPassword , password);
                if (result > 0)
                {
                    msg.AddBody(2);
                    msg.AddMessage(3);
                    msg.DefaultOperate(Url.Action("ChangePassword"));
                }
                else
                {
                    msg.AddMessage(2);
                }
            }
            return MessageResult(msg.CreateMessage());

        }

    }
}
